/* ******************************************************************************* *
 *   HASHTABLE.HH
 * ******************************************************************************* */

/* *
 * Copyright (C) 2005 Atul S. Vasu
 *
 * This program is free software; you cn redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 * */

/* *
 * Contact Details:
 *
 * mail
 *  Atul S. Vasu, Flat No. 3, Govt Quarters, Kowdiar,
 *  Thiruvananthapuram, Kerala, India. 695003.
 * email
 *        - atulsvasu@gmail.com
 *        - atulsv@cse.iitm.ernet.in (till early 2007).
 * */

/// Eval -type.
/**
 * Positional evaluation of board is stored in the hash_table
 * with the following details.
 */
#ifndef __HASHTABLE_H_
#define __HASHTABLE_H_

#ifndef _SINGLE_
#include <ext/hash_map>
#endif

struct eval{
  move Best;
  int sdepth;
  int Upper;
  int Lower;
  eval(move bestMove, int U, int L):Best(bestMove), sdepth(depth), Upper(U), Lower(L) {}
};

struct hasher {
  long operator () (const position& p) const{
    int b[8];
    for(int i=0;i<8;i++) {
	b[i]=0;
	for(int j=0;j<8;j++)
	  b[i]=b[i]*3+p.CB[i*n+j];
    }
    long h_e=0;
    for(int i=0;i<8;i++)
      h_e=h_e*5+b[i];
    return h_e;
  }
};

typedef __gnu_cxx::hash_map<position,eval,hasher> htable;
typedef htable::iterator hiter;

htable hash_store;
const int NO_HASH_DEPTH=0;

void hash_reset() {
  hash_store.clear();
}

void hash_commit(int Alpha, int Beta, move bestMove, int Score) {
  if(depth<=NO_HASH_DEPTH)
    return;

  hiter search=hash_store.find(En.P);

  int L, U;
  if (Score > Alpha) {
    L = Score;
    U = MAX;
  } else if (Score < Beta) {
    L = -MAX;
    U = Score;
  } else {
    L = Score;
    U = Score;
  }

  if(search==hash_store.end()) {
    std::pair<position,eval> x(En.P,eval(bestMove, L, U));
    hash_store.insert(x);
  } else {
    eval e=search->second;
    if(e.sdepth==depth) {
      e.Lower = std::min(e.Lower, L);
      e.Upper = std::max(e.Upper, U);
      search->second = e;
    }
    else if(e.sdepth<depth) {
      search->second=eval(bestMove, L, U);
    } else
      return; // Do nothing.
  }
}

/*
bool hash_decent(int Alpha, int Beta, int& Score) {
  hiter search=hash_store.find(En.P);
  if(search==hash_store.end())
    return false;

  /// RETURN EXACT SCORE
  if(search->second.Lower==search->second.Upper)
    {Score=search->second.Lower; return true;}

  /// PRUNABLE VALUE
  if(search->second.Lower>=Alpha)
    { Score=search->second.Lower; return true; }
  if(search->second.Upper<=Beta)
    { Score=search->second.Upper; return true; }

  return false;
} */
bool hash_bestMove(move& bestMove) {
  hiter search=hash_store.find(En.P);
  if(search==hash_store.end())
    return false;

  bestMove=search->second.Best;
  return true;
}

bool hash_search(int Alpha, int Beta, int& Score) {
  hiter search=hash_store.find(En.P);
  if(search==hash_store.end())
    return false;

  // TOO SMALL DEPTH
  if(search->second.sdepth<depth)
    return false;

  int L(search->second.Lower);
  int U(search->second.Upper);

  if (L == U) {
    Score = L;
    return true;
  }

  if (U < Beta) {
    Score = U;
    return true;
  } else if (L > Alpha) {
    Score = L;
    return true;
  }
  // Eval is not very useful yet.
  // Notice that this will happen in PVS.
  return false;
}

bool hash_sort(int & Score) {
  hiter search=hash_store.find(En.P);
  if(search==hash_store.end())
    return false;
  int L(search->second.Lower);
  int U(search->second.Upper);
  if (L == -MAX && U == MAX) {
    // No information :P
    return false;
  } else if (L == -MAX) {
    Score = U;
    return true;
  } else {
    Score = L;
    return true;
  }
}

#endif
